package com.edu.dk.security.impl;

import com.edu.dk.security.HeaderCommitter;
import com.edu.dk.security.WebContext;

/**
 * 保存commit的状态，用来防止重复commit。
 *
 */
public class CommitMonitor extends AbstractWebContextWrapper implements HeaderCommitter {
	private static final long serialVersionUID = -7686016385542154121L;
	private final FilterHandler handler;
	private boolean headersCommitted;
	private boolean committed;
	private WebContext topWebContext;

	public CommitMonitor(WebContext context, FilterHandler handler) {
		super(context);
		this.handler = handler;
	}

	public boolean isHeadersCommitted() {
		return headersCommitted || committed;
	}

	public void setHeadersCommitted(boolean headersCommitted) {
		this.headersCommitted = headersCommitted;
	}

	public boolean isCommitted() {
		return committed;
	}

	public void setCommitted(boolean committed) {
		this.committed = committed;
	}

	public WebContext getTopWebContext() {
		return topWebContext;
	}

	public void setTopWebContext(WebContext topWebContext) {
		this.topWebContext = topWebContext;
	}

	@Override
	/** 实现内部接口：<code>HeaderCommitter</code>。 */
	public void commitHeaders() {
		if (isHeadersCommitted()) {
			return;
		}
		handler.commitHeaders(topWebContext);
	}
}
